{% extends 'base.html' %}

{% block content %}
<div class="container mt-4">
    <h2>我的购物车</h2>
    {% if cart_items %}
    <table class="table">
        <thead>
            <tr>
                <th>商品</th>
                <th>单价</th>
                <th>数量</th>
                <th>小计</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            {% for item in cart_items %}
            <tr>
                <td>{{ item.product.name }}</td>
                <td>¥{{ item.product.price }}</td>
                <td>
                    <form action="{% url 'update_cart' item.id %}" method="post" class="d-flex align-items-center">
                        {% csrf_token %}
                        <input type="number" name="quantity" value="{{ item.quantity }}" min="1" class="form-control" style="width: 80px">
                        <button type="submit" class="btn btn-sm btn-secondary ms-2">更新</button>
                    </form>
                </td>
                <td>¥{{ item.subtotal }}</td>
                <td>
                    <form action="{% url 'remove_from_cart' item.id %}" method="post" style="display: inline;">
                        {% csrf_token %}
                        <button type="submit" class="btn btn-sm btn-danger">删除</button>
                    </form>
                </td>
            </tr>
            {% endfor %}
        </tbody>
        <tfoot>
            <tr>
                <td colspan="3" class="text-end"><strong>总计:</strong></td>
                <td><strong>¥{{ total }}</strong></td>
                <td></td>
            </tr>
        </tfoot>
    </table>
    <div class="text-end">
        <a href="{% url 'checkout' %}" class="btn btn-primary">去结算</a>
    </div>
    {% else %}
    <p>购物车是空的。 <a href="{% url 'product_list' %}">继续购物</a></p>
    {% endif %}
</div>
{% endblock %}